package com.liuym.student.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.liuym.common.dubbo.student.api.StudentDubboService;
import com.liuym.common.dubbo.student.dto.StudentDTO;
import com.liuym.common.exception.StudentException;
import com.liuym.student.repository.model.bo.StudentBO;
import com.liuym.student.service.StudentService;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiuYiMing
 * @date 2018/9/18 11:09
 * @describe：
 */
@Service(version = "${student.dubbo.version}")
public class StudentDubboServiceImpl implements StudentDubboService {

    @Autowired
    private StudentService studentService;


    @Override
    public StudentDTO getStudentByNumber(long studentNumber) throws StudentException {
        StudentBO studentBO = studentService.getStudentByNumber(studentNumber);
        if (studentBO != null) {
            StudentDTO studentDTO = BeanMapper.map(studentBO, StudentDTO.class);
            return studentDTO;
        }
        return null;
    }

    @Override
    public List<StudentDTO> getAllStudent() throws StudentException {
        List<StudentBO> studentBOList = studentService.getAllStudent();
        if (studentBOList != null && studentBOList.size() > 0) {
            // jdk1.8实现list类型转换
            List<StudentDTO> studentDTOList =
                    studentBOList
                            .stream()
                            .map(s -> BeanMapper.map(s, StudentDTO.class))
                            .collect(Collectors.toList());
            return studentDTOList;
        }
        return null;
    }

    @Override
    public List<StudentDTO> getStudentByClass(long classNumber) throws StudentException {
        List<StudentBO> studentBOList = studentService.getStudentByClass(classNumber);
        if (studentBOList != null && studentBOList.size() > 0) {
            // jdk1.8实现list类型转换
            List<StudentDTO> studentDTOList =
                    studentBOList
                            .stream()
                            .map(s -> BeanMapper.map(s, StudentDTO.class))
                            .collect(Collectors.toList());
            return studentDTOList;
        }
        return null;
    }

    @Override
    public int addStudent(StudentDTO studentDTO) throws StudentException {
        StudentBO studentBO = BeanMapper.map(studentDTO, StudentBO.class);
        return studentService.addStudent(studentBO);
    }

    @Override
    public Map<String, Object> addStudentList(List<StudentDTO> studentDTOList) throws StudentException {
        List<StudentBO> studentBOList =
                studentDTOList
                        .stream()
                        .map(s -> BeanMapper.map(s, StudentBO.class))
                        .collect(Collectors.toList());
        return studentService.addStudentList(studentBOList);
    }

    @Override
    public int deleteStudentByStudentNumber(long studentNumber) throws StudentException {
        return studentService.deleteStudentByStudentNumber(studentNumber);
    }

    @Override
    public int deleteStudentByClassNumber(long classNumber) throws StudentException {
        return studentService.deleteStudentByClassNumber(classNumber);
    }

    @Override
    public int updateStudent(StudentDTO studentDTO) throws StudentException {
        StudentBO studentBO = BeanMapper.map(studentDTO, StudentBO.class);

        return studentService.updateStudent(studentBO);
    }

    @Override
    public int updateScoreByStudentNumber(int score, long studentNumber) throws StudentException {
        return studentService.updateScoreByStudentNumber(score, studentNumber);
    }

    @Override
    public int updateStudentScoreTo80() throws StudentException {
        return studentService.updateStudentScoreTo80();
    }

    @Override
    public Integer getCountByClassNumber(long classNumber) {
        return studentService.getCountByClassNumber(classNumber);
    }
}
